﻿using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Net.Http.Headers;
namespace Zeus.Mvc
{
    using Resources;
    public static class ZeusExtensioncs
    {
        public static string HttpCorrelationId(this HttpContext httpContext)
        {
            return httpContext.Request.Headers.ContainsKey(HttpRequestHeader.HttpCorrelationId) ? httpContext.Request.Headers[HttpRequestHeader.HttpCorrelationId].ToString() : string.Empty;
        }

        public static string RemoteAddress(this HttpContext httpContext)
        {

            if (httpContext.Connection?.RemoteIpAddress != null)
            {
                return httpContext.Connection.RemoteIpAddress.ToString();
            }
            return string.Empty;
        }

        public static string ForwardedAddress(this HttpContext httpContext)
        {
            var forwardedForAll = httpContext.Request.Headers["X-Forwarded-For"];
            if (forwardedForAll.Count > 0)
            {
                return forwardedForAll.ToArray()[0];
            }
            return string.Empty;
        }

        public static string FindHeader(this IHeaderDictionary  keyValuePairs, string name) {
            if (keyValuePairs.ContainsKey(name))
            {
                return keyValuePairs[name];
            }
            return string.Empty;
        }
    }
}
